package com.example.musicplayer.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.musicplayer.R;
import com.example.musicplayer.activity.MusicListActivity;
import com.example.musicplayer.adapter.MusicListAdapter;
import com.example.musicplayer.entity.MusicList;
import com.example.musicplayer.util.DbUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 */
public class MineFragment extends Fragment implements View.OnClickListener {

    private static List<MusicList> mMusicLists;
    public View view;
    private ImageView mLike; // “喜欢”
    private ImageView mDownload; // “本地”
    private ImageView mMusicList; // “歌单”
    private ImageView mPurchase; // “已购”
    private DbUtil mDbUtil = new DbUtil();
    private MusicListAdapter adapter;
    private RecyclerView recyclerView;
    private LinearLayout mAddMusicList; // 添加歌单

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        mLike = view.findViewById(R.id.like);
        mDownload = view.findViewById(R.id.download);
        mMusicList = view.findViewById(R.id.my_music_list);
        mPurchase = view.findViewById(R.id.purchase);
        mAddMusicList = view.findViewById(R.id.add_music_list);
        mLike.setOnClickListener(this);
        mDownload.setOnClickListener(this);
        mMusicList.setOnClickListener(this);
        mPurchase.setOnClickListener(this);
        mAddMusicList.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        initMusicList();
        initRecyclerView(R.id.self_create_list, LinearLayoutManager.VERTICAL);
    }

    /**
     * 初始化RecyclerView
     */
    public void initRecyclerView(@IdRes int resId, @RecyclerView.Orientation int orientation) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getContext());
        if (orientation == LinearLayoutManager.VERTICAL) {
            linearLayoutManager.setOrientation(orientation);
            adapter = new MusicListAdapter(getContext(), mMusicLists, R.layout.recyclerview_music_list_item_vertical, "show");
        } else {
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            adapter = new MusicListAdapter(getContext(), mMusicLists, R.layout.recyclerview_music_list_item_horizontal, "show");
        }
        recyclerView = view.findViewById(resId);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);

    }

    /**
     * 初始化歌单列表
     */
    public void initMusicList() {
        mMusicLists = new ArrayList<>();
        if (getContext() != null) {
            mMusicLists = mDbUtil.getMusicList(getContext());
        }
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getContext(), MusicListActivity.class);
        switch (v.getId()) {
            case R.id.like:
                intent.putExtra("title", "我喜欢");
                intent.putExtra("request_code", "show");
                intent.putExtra("musicId", "null");
                startActivity(intent);
                break;
            case R.id.download:
                intent.putExtra("title", "本地音乐");
                intent.putExtra("request_code", "show");
                intent.putExtra("musicId", "null");
                startActivity(intent);
                break;
            case R.id.my_music_list:
                intent.putExtra("title", "歌单");
                intent.putExtra("request_code", "show");
                intent.putExtra("musicId", "null");
                startActivity(intent);
                break;
            case R.id.purchase:
                intent.putExtra("title", "已购");
                intent.putExtra("request_code", "show");
                intent.putExtra("musicId", "null");
                startActivity(intent);
                break;
            case R.id.add_music_list:
                createDialog();
                break;
            default:
                break;

        }
    }

    private void createDialog() {
        AddListDialogFragment dialog = new AddListDialogFragment(adapter, mMusicLists);
        if (getFragmentManager() != null) {
            dialog.show(getFragmentManager(), "ADD_LIST");
        }
    }
}
